package cn.exrick.xboot.modules.graphics.serviceimpl;

import cn.exrick.xboot.common.constant.CommonConstant;
import cn.exrick.xboot.common.exception.ServiceException;
import cn.exrick.xboot.factory.MyBeanFactory;
import cn.exrick.xboot.modules.graphics.dao.mapper.GraphicsContainerMapper;
import cn.exrick.xboot.modules.graphics.entity.Container;
import cn.exrick.xboot.modules.graphics.entity.GraphicsContainer;
import cn.exrick.xboot.modules.graphics.enums.GraphicsPageExceptionEnum;
import cn.exrick.xboot.modules.graphics.param.GraphicsPageParam;
import cn.exrick.xboot.modules.graphics.service.IContainerService;
import cn.exrick.xboot.modules.graphics.service.IGraphicsContainerService;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 画布容器中间表接口实现
 * @author njp
 */
@Slf4j
@Service
@Transactional
public class IGraphicsContainerServiceImpl extends ServiceImpl<GraphicsContainerMapper, GraphicsContainer> implements IGraphicsContainerService {
    @Autowired
    private IContainerService iContainerService;

    @Override
    public List<GraphicsContainer> getGraphicsContainerListByGraphicsId(String graphicsId){
        LambdaQueryWrapper<GraphicsContainer> queryWrapper=new  LambdaQueryWrapper();
        queryWrapper.eq(GraphicsContainer::getGraphicsId,graphicsId);
        //查询未删除的
        queryWrapper.ne(GraphicsContainer::getDelFlag, CommonConstant.DEL_FLAG);
        return this.list(queryWrapper);
    }

    @Override
    public boolean deleteByGraphicsId(String graphicsId) {
        LambdaUpdateWrapper<GraphicsContainer> updateWrapper=new  LambdaUpdateWrapper();
        updateWrapper.eq(GraphicsContainer::getGraphicsId,graphicsId);
        updateWrapper.eq(GraphicsContainer::getDelFlag,CommonConstant.STATUS_NORMAL);
        updateWrapper.set(GraphicsContainer::getDelFlag,CommonConstant.DEL_FLAG);
        return this.update(updateWrapper);
    }

    @Override
    public boolean add(GraphicsContainer graphicsContainer,String graphicsId) {
        //检查参数 todo
        Container container =null;
        if(ObjectUtil.isNotNull(graphicsContainer.getContainerId())&&!"".equals(graphicsContainer.getContainerId().trim())) {
            //查询容器数据
            container = iContainerService.getById(graphicsContainer.getContainerId());
            if (container == null) {
                throw new ServiceException(GraphicsPageExceptionEnum.CONTAINER_NOT_EXIST);
            }
        }
        MyBeanFactory myBeanFactory=MyBeanFactory.getMyBeanFactory();
        GraphicsContainer gc = myBeanFactory.createBean(GraphicsContainer.class);
        //设置界面id
        gc.setGraphicsId(graphicsId);
        CopyOptions copyOptions = new CopyOptions();
        //设置拷贝时忽略null值
        copyOptions.setIgnoreNullValue(true);
        //graphicsContainer.setId(null);
        copyOptions.setIgnoreProperties("id");
        BeanUtil.copyProperties(graphicsContainer, gc,copyOptions);
        //设置容器基础信息到中间表中
        if(container!=null) {
            gc.setCTitle(container.getTitle());
            gc.setContent(container.getContent());
        }
        boolean flag = this.save(gc);
        return flag;
    }

    @Override
    public List<GraphicsContainer> getGraphicsContainerListByContainerId(String containerId) {
        LambdaQueryWrapper<GraphicsContainer> queryWrapper=new LambdaQueryWrapper();
        if(ObjectUtil.isNotNull(containerId)) {
            queryWrapper.eq(GraphicsContainer::getContainerId, containerId);
        }else{
            return  null;
        }
        return this.list(queryWrapper);
    }

    /**
     * njp
     * 校验参数，判断状态是否正确
     */
    private void checkParam(GraphicsPageParam GraphicsContainerParam, int serviceType, boolean isExcludeSelf){
        //todo
    }

}